package de.ventu.handler
{
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.TextArea;
	import mx.controls.Spacer;
	import mx.core.Application;
	import mx.utils.StringUtil;
	
	
	public class TagareaEventHandler
	{
		private var _tagArea:TextArea;
		private var _tagfieldBar:DisplayObjectContainer;
		
		public function TagareaEventHandler(tagArea:TextArea, tagfieldBar:DisplayObjectContainer)
		{
			_tagArea = tagArea;
			_tagfieldBar = tagfieldBar;
			
			Application.application.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEventHandler);
		}

		private function keyDownEventHandler(event:KeyboardEvent):void
		{
			if(Application.application.captureTagAreaInput)
			{
				if(event.keyCode == Keyboard.SPACE || event.keyCode == Keyboard.COMMA || 
				   (event.keyCode == 190 && event.shiftKey) ||
				   (event.keyCode == 226 && event.shiftKey) ||
				   (event.keyCode >= Keyboard.A && event.keyCode <= Keyboard.Z) ||
				   (event.keyCode >= Keyboard.NUMPAD_0 && event.keyCode <= Keyboard.NUMPAD_9) ||
				   (event.keyCode >= Keyboard.NUMBER_0 && event.keyCode <= Keyboard.NUMBER_9))
				{
					_tagArea.text += String.fromCharCode(event.charCode);
				}
				else if(event.keyCode == Keyboard.BACKSPACE)
				{
					if(_tagArea.text.length != 0)
					{
						var temp:String = _tagArea.text;
						_tagArea.text = temp.substring(0, temp.length - 1);	
					}
				}
				else if(event.keyCode == Keyboard.ENTER)
				{
					if(checkForWhitespaceAndLength(_tagArea.text))
						tagfieldHierarchy(splitString(_tagArea.text));
						
					_tagArea.text = '';
				}
			}
		}
				
		private function splitString(string:String):Array
		{
			var result:Array = new Array();
			
			var tempGreaterThen:Array
			var greaterThen:Array;
			
			if(string.indexOf(">") != -1)
			{
				tempGreaterThen =  string.split(">");
				greaterThen = new Array();
				
				for each(var greaterThenElement:String in tempGreaterThen)
				{
					if(checkForWhitespaceAndLength(greaterThenElement))
						greaterThen.push(greaterThenElement);
				}
			}
			else
			{
				greaterThen = [string];
			}
			
			var tempComma:Array;
			var comma:Array;
			
			for each(var element:String in greaterThen)
			{
				if(element.indexOf(",") != -1)
				{
					tempComma = element.split(",");
					comma= new Array();
					
					for each(var commaElement:String in tempComma) 
					{
						if(checkForWhitespaceAndLength(commaElement))
							comma.push(commaElement);
					}
					result.push(comma);
				}
				else
				{
					result.push([element]);
				}
			}			
			
			return result;
		}
		
		private function checkForWhitespaceAndLength(elment:String):Boolean
		{
			var tempString:String = StringUtil.trim(elment);
			if(tempString.length != 0 && !StringUtil.isWhitespace(tempString))
				return true;
			else
				return false;	
		}
		
		private function tagfieldHierarchy(stringArray:Array):void
		{
			// i = hierarchy level
			for(var i:int = 0; i < stringArray.length; i++)
			{
				for each(var element:String in stringArray[i])
				{
					createTagfield(i, StringUtil.trim(element), stringArray.length);
				}
			}
			
		}
		
		private function createTagfield(level:int, label:String, hierarchyLevels:int):void
		{
			var hBox:HBox = new HBox();
			hBox.setStyle("verticalGap", 0);
			hBox.setStyle("horizontalGap", 0);
			
			
			var tagfield:Button = new Button();
			tagfield.label = level.toString() + " " +label;
			tagfield.width = 160;
			
			if (hierarchyLevels == 1)
			{
				tagfield.styleName = "tagfieldLSingle";
			}
			else
				tagfield.styleName = "tagfieldLevel"+level;
			
			
			
			
			hBox.addChild(tagfield);

			var makeFolder:Button = new Button();
			makeFolder.styleName = "toggleFolderBTN";
			makeFolder.toggle = true;
			makeFolder.label = "250";
			
			hBox.addChild(makeFolder);

			_tagfieldBar.addChild(hBox);
			
			trace(hierarchyLevels, level);
			
			if (level == hierarchyLevels-1 && level !=0)
			{
				trace("spacer");
				var hierarchyEnd:Spacer= new Spacer();
				hierarchyEnd.height=8;
				_tagfieldBar.addChild(hierarchyEnd);
			}
		}
	}
}